
import java.util.*;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class WPDLXML{
    public static final String ELEMENTWORKFLOW="WORKFLOW";
    public static final String ELEMENTACTIVITY="ACTIVITY";
    public static final String ELEMENTTRANSITION="TRANSITION";
    public static final String ELEMENTDESCRIPTION="DESCRIPTION";
    public static final String ELEMENTDATA="DATA";
    public static final String ELEMENTROUTE="ROUTE";
    public static final String ELEMENTIMPLEMENTATION="IMPLEMENTATION";
    public static final String ELEMENTNO="NO";
    public static final String ELEMENTAPPLICATIONS="APPLICATIONS";
    public static final String ELEMENTSUBWORKFLOW="SUBWORKFLOW";
    public static final String ELEMENTLOOP="LOOP";
    public static final String ELEMENTPARAMETERS="PARAMETERS";
    public static final String ELEMENTTOOLREF="TOOLREF";
    public static final String ELEMENTRESTRICTTO="RESTRICT_TO";

    public static final String ATTRIBUTEID="ID";
    public static final String ATTRIBUTEVERSION="VERSION";
    public static final String ATTRIBUTECLASSIFICATION="CLASSIFICATION";
    public static final String ATTRIBUTEIDREF="IDREF";
    public static final String ATTRIBUTENAME="NAME";
    public static final String ATTRIBUTEFROM="FROM";
    public static final String ATTRIBUTETO="TO";
    public static final String ATTRIBUTECONDITION="CONDITION";
    public static final String ATTRIBUTETYPE="TYPE";
    public static final String ATTRIBUTEVALUE="VALUE";
    public static final String ATTRIBUTEDEFAULTVALUE="DEFAULT_VALUE";
    public static final String ATTRIBUTESPLIT="SPLIT";
    public static final String ATTRIBUTEJOIN="JOIN";
    public static final String ATTRIBUTESTARTMODE="START_MODE";
    public static final String ATTRIBUTEFINISHMODE="FINISH_MODE";
    public static final String ATTRIBUTEPERFORMER="PERFORMER";
    public static final String ATTRIBUTEPRIORITY="PRIORITY";
    public static final String ATTRIBUTEIN="IN";
    public static final String ATTRIBUTEOUT="OUT";
    public static final String ATTRIBUTEPARAMETERLIST="PARAMETERLIST";
    public static final String ATTRIBUTEACTIVITYINSTANCE="ACTIVITY_INSTANCE";
    public static final String ATTRIBUTEVALUEAND="AND";
    public static final String ATTRIBUTEVALUEXOR="XOR";
    public static final String ATTRIBUTEVALUEMANUAL="MANUAL";
    public static final String ATTRIBUTEVALUEAUTOMATIC="AUTOMATIC";

    public static final String PLAINDATATYPESTRING="STRING";

    public static Document FileRootDocumentGet(String FileName) throws Exception{
		Document TheDocument=null;
		dom.DOMParserWrapper parser = new dom.wrappers.DOMParser ();
		TheDocument = parser.parse(FileName);
		return(TheDocument);
    }

    public static Node DocumentNodeGet(Document TheDocument){
	Node theNode=null;
	theNode = TheDocument.getDocumentElement();
	return(theNode);
    }

    public static Node[] NodeElementSelectManyGivenName(Node TheNode,
							String TheName){
	int count = 0;
	Node[] SelectedNodes=null;

	NodeList children = TheNode.getChildNodes();

	if (children != null) {
	    int len = children.getLength();
	    for (int i = 0; i < len; i++) {
		short nodeType=children.item(i).getNodeType();
		Node oneChild=children.item(i);
		if (nodeType == Node.ELEMENT_NODE){
		    String nodeName = oneChild.getNodeName();
		    if(nodeName.equals(TheName)){
			count++;
		    }
		}
	    }

	    SelectedNodes = new Node[count];
	    count = 0;
	    for (int i = 0; i < len; i++) {
		short nodeType=children.item(i).getNodeType();
		Node oneChild=children.item(i);
		if (nodeType == Node.ELEMENT_NODE){
		    String nodeName = oneChild.getNodeName();
		    if(nodeName.equals(TheName)){
			SelectedNodes[count] = oneChild; count++;
		    }
		}
	    }

	}

	return(SelectedNodes);
    }

    public static String NodeAttributeValueGet(Node TheNode, 
					       String AttributeName){
	String theValue = null;
	NamedNodeMap attrs = TheNode.getAttributes();
	Attr nameAttribute = (Attr)attrs.getNamedItem(AttributeName);
	if(nameAttribute != null) theValue = nameAttribute.getValue();
	return theValue;
    }

    public static Node[] NodeSelectManyGivenAttributeValue(Node[] Nodes, 
							 String AttributeName, 
							   String Value){
	Node[] selectedNodes = null;
	Vector selectedNodesVector = new Vector();
	int count=0;

	int len = Nodes.length;
	for(int i = 0; i < len; i++){
	    String AttributeValue = NodeAttributeValueGet(Nodes[i],
							  AttributeName);
	    if(AttributeValue.equals(Value)){
		selectedNodesVector.addElement(Nodes[i]);
	    }
	}
	selectedNodes = new Node[len = selectedNodesVector.size()];

	for(int i = 0; i < len; i++){
	    selectedNodes[i] = (Node)selectedNodesVector.elementAt(i);
	}
	
	return selectedNodes;
    }

    public static Node FileWorkflowNodeSelectGivenID(String FileName,
						     String WorkflowID)
    throws Exception{

	Document RootDocument = FileRootDocumentGet(FileName);
	Node DocumentNode = DocumentNodeGet(RootDocument);
	Node[] WorkflowNodes = NodeElementSelectManyGivenName(DocumentNode,
							      ELEMENTWORKFLOW);
	Node[] theWorkflowArray = 
	    NodeSelectManyGivenAttributeValue(WorkflowNodes,
					      ATTRIBUTEID, 
					      WorkflowID);
	if(theWorkflowArray.length != 1){
	    return null;
	}
	return theWorkflowArray[0];
    }

    public static Node FileActivityNodeSelectGivenID(String FileName,
						     String WorkflowID,
						     String ActivityID)
    throws Exception{

	Node theWorkflowNode = 
	    FileWorkflowNodeSelectGivenID(FileName, WorkflowID);
	Node[] ActivityNodes = 
	    NodeElementSelectManyGivenName(theWorkflowNode, ELEMENTACTIVITY);
	Node[] theActivityArray = 
	    NodeSelectManyGivenAttributeValue(ActivityNodes,
					      ATTRIBUTEID, 
					      ActivityID);
	if(theActivityArray.length != 1){
	    return null;
	}
	return theActivityArray[0];
    }

   public static Node FileTransitionNodeSelectGivenID(String FileName,
						     String WorkflowID,
						     String TransitionID)
    throws Exception{

	Node theWorkflowNode = 
	    FileWorkflowNodeSelectGivenID(FileName, WorkflowID);
	Node[] TransitionNodes = 
	    NodeElementSelectManyGivenName(theWorkflowNode, ELEMENTTRANSITION);
	Node[] theTransitionArray = 
	    NodeSelectManyGivenAttributeValue(TransitionNodes,
					      ATTRIBUTEID, 
					      TransitionID);
	if(theTransitionArray.length != 1){
	    return null;
	}
	return theTransitionArray[0];
    }

}
